#!/usr/bin/env bash

rm -rf /tmp/release
mkdir -p /tmp/release
cp bb /tmp/release

VERSION=$(cat resources/BABASHKA_VERSION)



## release binary as tar.gz archive

arch=${BABASHKA_ARCH:-amd64}

if [ "$BABASHKA_STATIC" = "true" ]; then
    arch="$arch-static"
fi

# because circle won't allow the same file to be saved/restored in the same workspace concurrently
cp metabom.jar "/tmp/release/$BABASHKA_PLATFORM-$arch-metabom.jar"

cd /tmp/release
mkdir -p /tmp/bb_size
./bb '(spit "/tmp/bb_size/size" (.length (io/file "bb")))'

archive="babashka-$VERSION-$BABASHKA_PLATFORM-$arch.tar.gz"

tar zcvf "$archive" bb # bbk

cd -

if [ "$BABASHKA_RELEASE" = "true" ]; then
    ./bb --config .build/bb.edn --deps-root . release-artifact "/tmp/release/$archive"
fi

## cleanup

cd /tmp/release
rm bb # bbk
